# 请设计一个抢红包系统，例如AI一组共13人，每人必须发一次，顺序任意。当A同学发红包时其余同学抢红包，每一个同学发完红包后输出本轮抢红包的运气王和倒霉王以及没抢到红包的组员。所有人都发完红包后再得出这个游戏的运气王和倒霉王。
# 即共输出14次运气王和倒霉王。这13名同学有自己的总金额total，发红包时的个数num(个数就是有num个人能抢到红包)
# 自定。每个同学发红包时的总金额payment自定，payment必然小于total，抢到的红包总金额get_money。
#
# 模拟你们组成员（至少五个成员）抢红包游戏（随机成员获得随机金额），最后输出没抢到红包的成员以及运气王和倒霉王。
# 在所有成员的红包发放完毕后，输出抢到红包最多的运气王，以及可怜的倒霉王。
#
# 要求：
# 每个用户的红包个数num属性不允许在类的外部被使用
# 每个红包里的金额随机生成，并保留两位小数；总金额由用户输入
# 在每个成员发放红包时：得到红包的成员随机生成，每位成员最多只可以抢到一个红包
# 其余要求结合实际抢红包规则
import random


class RedPaper:

    def __init__(self, payment=8 * 0.01, num=8):
        self.payment = payment
        self.num = num


class People:
    allPeople = []
    allPeopleObject = []

    def __init__(self, name, total, get_money=0):
        self.name = name
        self.total = total
        self.get_money = get_money
        self.__redPaper = RedPaper()
        People.allPeople.append([self.name, self.total, self.get_money])

    @classmethod
    def arrange(cls):
        for personOrder in range(len(People.allPeople)):
            print(f"第{personOrder + 1}轮，由{People.allPeople[personOrder][0]}同学发红包")
            # 本轮游戏抢到红包的人的情况(姓名、抢到的金额数)
            winPeople = []
            # 本次红包能抢到的人数
            num = random.randint(1, len(People.allPeople) - 1)
            print("本次红包个数%d:" % num)
            # 获取每个参与者本轮发红包的随机金额
            payment = round(random.uniform(0.01 * num, People.allPeople[personOrder][1]), 2)
            print("本次红包总金额%.2f" % payment)

            # 发红包的同学总金额total变化
            People.allPeople[personOrder][1] -= payment
            People.allPeople[personOrder][1] = round(People.allPeople[personOrder][1], 2)

            # # 将红包实例作为People类的一个参数
            # self.redPaper = RedPaper(payment, num)
            # 随机出本轮抢到红包的同学序号
            for n in range(num):
                winOrder = random.randint(0, len(People.allPeople) - 1)
                # 确保每人只能抢一次红包 确保自身不能抢到红包
                while [winOrder] in winPeople or winOrder == personOrder:
                    winOrder = random.randint(0, len(People.allPeople) - 1)
                winPeople.append([winOrder])
                # [[1,1.22],[3],...]
            # 随机出各自抢到的金额,m为第几个抢到红包的同学
            for m in range(1, num + 1):
                if m != num:
                    luckyMoney = round(random.uniform(0.01, payment - (num - m) * 0.01), 2)
                    payment -= luckyMoney
                else:
                    luckyMoney = round(payment, 2)
                # print(luckyMoney)
                winPeople[m - 1].append(luckyMoney)

            # 游戏总列表变化
            for j in range(num):
                # 抢到红包的同学的get_Money增加
                People.allPeople[winPeople[j][0]][2] += winPeople[j][1]
                # 抢到红包的同学的total增加
                People.allPeople[winPeople[j][0]][1] += winPeople[j][1]

            # 找出倒霉王和运气王(只输出一个)
            winPeople.sort(key=takeSecond, reverse=True)
            print("运气王是：%s  编号是：%d" % (People.allPeople[winPeople[0][0]][0], winPeople[0][0]))
            print("倒霉王是：%s  编号是：%d" % (People.allPeople[winPeople[-1][0]][0], winPeople[-1][0]))
            print(winPeople)
            print(People.allPeople)
        return winPeople


def takeSecond(elem):
    return elem[1]


def takeThird(elem):
    return elem[2]


if __name__ == '__main__':
    allName = ['张玉洁', '刘珊池', '韩丽璇', '连芮', '薛晓菲', '张一乐', '边金瑞', '冀港', '王洋', '韦双迎', '张瑞宏', '张维加', '王士博']
    for i in range(len(allName)):
        new_people = People(allName[i], round(random.uniform(1, 10), 2))
    print(People.allPeople)
    print()
    print(new_people.arrange())
    print(People.allPeople)
    People.allPeople.sort(key=takeThird, reverse=True)
    print('\n\n')
    print(People.allPeople)
    print("本次游戏运气王：%s" % People.allPeople[0][0])
    print("本次游戏倒霉王：%s" % People.allPeople[-1][0])


